package com.xgjweb.common.service;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import com.xgjweb.common.utils.XgjWebLogger;
import com.xgjweb.common.utils.XgjWebUtils;

public class CommonServiceImpl implements ICommonService {
	protected static final Boolean SERVICE_SUCCESS = true;
	protected static final Boolean SERVICE_FAIL = false;
	public static final String ALERT_SCRIPT = "alertScript";
	protected static final String SUCCESS = "success";
	protected static final String ERROR = "error";

	/**
	 * 写消息，基于js 的alter
	 * 
	 * @param request
	 * @param msg
	 */
	protected void msgByAlert(HttpServletRequest request, String msg) {
		StringBuffer sb = new StringBuffer("<script>alert('");
		sb.append(msg).append("');</script>");
		request.setAttribute(ALERT_SCRIPT, sb.toString());
	}

	/**
	 * 写消息，基于flex
	 * 
	 */
	protected String successForFlex(String msg) {
		return SUCCESS + "_" + msg;
	}

	/**
	 * 写消息，基于flex
	 * 
	 */
	protected String errorForFlex(String msg) {
		return ERROR + "_" + msg;
	}

	/**
	 * 初始化标记
	 * 
	 * @param request
	 */
	protected void queryInitFlag(HttpServletRequest request) {
		request.setAttribute("initFlag", "initFlag");
	}

	/**
	 * 根据key得打当前的session的对象
	 * 
	 * @param request
	 * @param key
	 * @return
	 */
	protected Object valueBySession(HttpServletRequest request, String key) {
		return request.getSession().getAttribute(key);
	}

	/**
	 * 根据key得打当前的request的参数
	 * 
	 * @param request
	 * @param key
	 * @return
	 */
	protected String parmentByRequest(HttpServletRequest request, String key) {
		return request.getParameter(key);
	}

	/**
	 * 转换字符串
	 * 
	 * @param request
	 * @param key
	 * @return
	 */
	protected String ReEncode(String str) throws Exception {
		try {
			if (str == null)
				return "";
			return new String(str.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			XgjWebLogger.getInstance().error("字符串转换错误", e);
			throw e;
		}
	}
}
